// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const FileManagerPlugin = require('filemanager-webpack-plugin');

const isProduction = process.env.NODE_ENV === "production";
const isPrivateProduction = process.env.NODE_ENV === "production:private";
const extensionName = isProduction ? 'name' : 'nameDev';
const { version: crxVersion } = require('./src/manifest.json');

const config = {
  optimization: {
    splitChunks: {
      minSize: 1000 * 1000 * 1000,
    }
  },
  entry: {
    'content/index': './src/content/index.tsx',
    'background': './src/background/index.ts',
    'options/index': './src/options/index.tsx',
    'popup/index': './src/popup/index.tsx',
    // 'popup/askpermission': './src/popup/AskPermission/index.tsx',
    // 'content/interceptor': './src/content/interceptor/index.ts'
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    clean: true
  },
  devServer: {
    open: true,
    host: "localhost",
  },
  plugins: [
    new CopyPlugin({
      patterns: [
        {
          from: 'src/_locales/',
          to: '_locales/'
        },
        {
          from: './src/static/',
          to: 'static/'
        },
        {
          from: './src/static/images/favicon.ico',
          to: 'favicon.ico'
        },
        {
          from: './src/manifest.json',
          to: 'manifest.json',
          transform(content) {
            return content
              .toString()
              .replace(/"name": "__MSG_[a-zA-Z]+__"/, `"name": "__MSG_${extensionName}__"`);
          },
        }
      ]
    }),
    new HtmlWebpackPlugin({
      template: "./src/options/index.html",
      filename: 'options/index.html',
      chunks: ["options/index"]
    }),
    new HtmlWebpackPlugin({
      template: "./src/popup/index.html",
      filename: 'popup/index.html',
      chunks: ["popup/index"]
    }),
    // new HtmlWebpackPlugin({
    //   template: "./src/popup/AskPermission/index.html",
    //   filename: 'popup/askpermission.html',
    //   chunks: ["popup/askpermission"]
    // }),

    new MiniCssExtractPlugin(),

    // Add your plugins here
    // Learn more about plugins from https://webpack.js.org/configuration/plugins/
  ],
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/i,
        loader: "ts-loader",
        exclude: ["/node_modules/"],
      },
      {
        test: /\.css$/i,
        use: [{ loader: 'style-loader', options: { insert: 'html', injectType: 'singletonStyleTag' } }, "css-loader"],
      },
      {
        test: /\.less$/i,
        use: [{ loader: 'style-loader', options: { insert: 'html', injectType: 'singletonStyleTag' } }, "css-loader", {
          loader: "less-loader",
          options: {
            lessOptions: {
              javascriptEnabled: true
            }
          }
        }],

      },
      {
        test: /\.(scss|sass)$/i,
        use: [{ loader: 'style-loader', options: { insert: 'html', injectType: 'singletonStyleTag' } }, "css-loader", "sass-loader"],
      },
      {
        test: /\.(svg|png|jpg|gif|jpeg)$/i,
        type: "asset/inline",
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/inline'

      },

      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
};

module.exports = () => {
  if (isProduction) {
    config.mode = "production";
    config.plugins.push(
      new FileManagerPlugin({
        events: {
          onEnd: {
            archive: [{ source: './dist', destination: `./history_package/pro/pro_${crxVersion}.zip` }]
          }
        }
      })
    );
  } else if (isPrivateProduction) {
    config.mode = "production";
    config.plugins.push(
      new FileManagerPlugin({
        events: {
          onEnd: {
            archive: [{ source: './dist', destination: `./history_package/dev/dev_${crxVersion}.zip` }]
          }
        }
      })
    );
  } else {
    config.mode = "development";
    // 防止eval()触发csp
    config.devtool = "inline-source-map";
  }
  return config;
};
